package com.bbx.flink.demo.window;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

public class SlidingWindowDemo {

    public static void  main(String [] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> socketTextStream = env.socketTextStream("192.168.10.131", 10003);
        SingleOutputStreamOperator<Integer> sum = socketTextStream.map(new MapFunction<String, Integer>() {
            private static final long serialVersionUID = 7850455640573754444L;

            @Override
            public Integer map(String value) throws Exception {
                return Integer.parseInt(value);
            }
        })
                .keyBy(i -> i % 2)
                .window(SlidingProcessingTimeWindows.of(Time.minutes(2),Time.minutes(1)))
//                .timeWindow(Time.minutes(2), Time.minutes(1))
                .sum(0);

        sum.print();
        env.execute();

    }

}
